package tools

import (
	"strings"
	"time"
)

const (
	YYYYMMDD       = "2006-01-02"
	YYYYMMDDHHmmSS = "2006-01-02 15:04:05"
	YYYYMMDDHHmm   = "2006.01.02 15:04"
	YMDHm          = "2006-01-02 15:04"
	YMDHms         = "2006.01.02 15:04:05"
	MMDD           = "01.02"
	MD             = "01月02日"
	HHmm           = "15:04"

	YyYyMmDdHHmmSS = "2006年01月02日15:04"
	MmDdHHmmSS     = "01月02日15:04"
	// NowIntegralPoint 当前时间整点
	NowIntegralPoint = "2006-01-02 15:00"
)

func StringToTime(myTime, format string) time.Time {
	parse, _ := time.ParseInLocation(format, myTime, time.Local)
	return parse
}

func TimeToString(myTime time.Time, format string) string {
	return myTime.Format(format)
}

func TimeParseInt64(tstamp int64) time.Time {
	return time.Unix(tstamp, 0)
}
func TimeUnixShowLayoutString(t int64) string {
	return TimeParseInt64(t).Format(YYYYMMDDHHmmSS)
}
func TimeShowLayoutString(t time.Time) string {
	return t.Format(YYYYMMDDHHmmSS)
}

func TimePushDayToString(t time.Time, day int) string {
	return t.AddDate(0, 0, day).Format(YYYYMMDDHHmmSS)
}

func DateTimeToDate(t time.Time) time.Time {
	return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
}

// HourMinToTime 时分转时间 (时分 HHmm, 年月日 yyyyMMdd)
func HourMinToTime(HHmm time.Time, yyyyMMdd time.Time) time.Time {
	return time.Date(yyyyMMdd.Year(), yyyyMMdd.Month(), yyyyMMdd.Day(), HHmm.Hour(), HHmm.Minute(), HHmm.Second(), 0, time.Local)
}

// TimeEqTimeNotTz 比较两个时间去掉时区
func TimeEqTimeNotTz(t, t2 time.Time) bool {

	return strings.EqualFold(t.Format(YYYYMMDD), t2.Format(YYYYMMDD))
}

// WeekStartTime 获取该时间的星期开始时间
func WeekStartTime(t time.Time) time.Time {
	offset := int(time.Monday - t.Weekday())
	if t.Weekday() == 0 {
		offset = -6
	}
	startTime := DateTimeToDate(t.AddDate(0, 0, offset))
	return startTime
}

// MonthStartTime 获取该时间的月份开始时间
func MonthStartTime(t time.Time) time.Time {
	day := t.Day()
	startTime := DateTimeToDate(t.AddDate(0, 0, -day+1))
	return startTime
}
